package civicinity.ist.psu.edu;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.facebook.android.Facebook;

import civicinity.ist.psu.edu.constants.Constants;
import civicinity.ist.psu.edu.events.CivicinityCalFeed;
import civicinity.ist.psu.edu.events.CivicinityEvents;
import civicinity.ist.psu.edu.events.CivicinityGallery;
import civicinity.ist.psu.edu.facebook.SessionStore;
import civicinity.ist.psu.edu.profile.CivicinityPreferences;
import civicinity.ist.psu.edu.rssfeed.RSSFeed;
import civicinity.ist.psu.edu.rssfeed.RSSFeedService;
import civicinity.ist.psu.edu.rssfeed.RSSReader;
import civicinity.ist.psu.edu.share.CivicinityShareListview;
import civicinity.ist.psu.edu.share.CivicinityShareMapview;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CivicinityMainpage extends Activity {

	private Button newsBtn;
	private Button eventsBtn;
	private Button shareBtn;
	private Button profileBtn;
	private TextView usernameTxt;
	private ImageView facebookLoginView;
	
	private SharedPreferences mPref;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.civicinity_mainpage);
        
        mPref = PreferenceManager.getDefaultSharedPreferences(this);
        
        newsBtn = (Button) findViewById(R.id.news_btn);
        eventsBtn = (Button) findViewById(R.id.events_btn);
        shareBtn = (Button) findViewById(R.id.user_event_btn);
        profileBtn = (Button) findViewById(R.id.user_profile_btn);
        usernameTxt = (TextView) findViewById(R.id.footer_text);
        facebookLoginView = (ImageView) findViewById(R.id.facebook_login);
        
        usernameTxt.setText("Hello, " + mPref.getString("username", "Guest"));
        
        if(mPref.getBoolean("facebook", true)) {
        	facebookLoginView.setVisibility(View.VISIBLE);
        }
        
        newsBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(CivicinityMainpage.this, RSSReader.class));
			}
		});
        
        Date date = Calendar.getInstance().getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("MMM dd");
        eventsBtn.setText("" + sdf.format(date));
        
        eventsBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast.makeText(CivicinityMainpage.this, "Coming soon!", Toast.LENGTH_SHORT).show();
				//startActivity(new Intent(CivicinityMainpage.this, CivicinityEvents.class));
				startActivity(new Intent(CivicinityMainpage.this, CivicinityEvents.class));
			}
		});
        
        shareBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast.makeText(CivicinityMainpage.this, "Coming soon!", Toast.LENGTH_SHORT).show();
				startActivity(new Intent(CivicinityMainpage.this, CivicinityShareListview.class));
				//startActivity(new Intent(CivicinityMainpage.this, CivicinityShareMapview.class));
			}
		});
        
        profileBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(CivicinityMainpage.this, CivicinityPreferences.class));
				finish();
			}
		});
        
    }
    
    // or when user press back button
	// when you hold the button for 3 sec, the app will be exited
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == event.KEYCODE_BACK) {

			new AlertDialog.Builder(CivicinityMainpage.this)
			.setTitle("Exit")
			.setMessage("Exit Civicinity?")
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					// out of the application
					finish();
				}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					// nothing to do
				}
			})
			.show();
		}
		
		return false;
	}
}